#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "unity.h"
#include "mock_bsp_test_commom.h"  // Mock头文件
#include "B1553_CMDHandle.h"

// 测试夹具
static B1553_BusService busService;
static u8 retData[1000];
static u32 retDataLen;

void setUp(void) {
    // 初始化测试环境
    memset(&busService, 0, sizeof(busService));
    memset(retData, 0, sizeof(retData));
    retDataLen = 0;
}

void tearDown(void) {
    // 清理测试环境
}

// TC001: 测试rt_CMDProc返回负值的情况
void test_B1553_CMDHandle_should_Return_CMDHANDLE_ERR_When_rt_CMDProc_Returns_Negative(void) {
    // Arrange
    rt_CMDProc_ExpectAndReturn(&busService, -1);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_ERR, result);
}

// TC002: 测试rt_CMDProc返回0的情况
void test_B1553_CMDHandle_should_Return_CMDHANDLE_DONE_When_rt_CMDProc_Returns_Zero(void) {
    // Arrange
    rt_CMDProc_ExpectAndReturn(&busService, 0);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_DONE, result);
}

// TC003: 测试处理遥测发送(计数器未溢出)
void test_B1553_CMDHandle_should_Handle_Telemetry_Transmission_When_Msg_TMTx_cnt_Greater_Than_Zero_And_Count_Not_Overflow(void) {
    // Arrange
    busService.Msg_TMTx_cnt = 1;
    busService.Telemeter_TX_Data.Comm_Telemeter_TX_Count = 100;
    rt_CMDProc_ExpectAndReturn(&busService, 5);
    rt_SendTelemetry_Expect(&busService);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(4, result);  // 5-1
    TEST_ASSERT_EQUAL_INT(101, busService.Telemeter_TX_Data.Comm_Telemeter_TX_Count);
    TEST_ASSERT_EQUAL_INT(0, busService.Msg_TMTx_cnt);
    TEST_ASSERT_EQUAL_INT(0, retDataLen);
    // 注意：在C语言中无法直接验证pRetData是否为NULL，因为它是局部变量的副本
}

// TC004: 测试处理遥测发送(计数器溢出)
void test_B1553_CMDHandle_should_Reset_Count_And_Handle_Telemetry_Transmission_When_Msg_TMTx_cnt_Greater_Than_Zero_And_Count_Overflow(void) {
    // Arrange
    busService.Msg_TMTx_cnt = 1;
    busService.Telemeter_TX_Data.Comm_Telemeter_TX_Count = 0xFFFF;
    rt_CMDProc_ExpectAndReturn(&busService, 3);
    rt_SendTelemetry_Expect(&busService);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(2, result);  // 3-1
    TEST_ASSERT_EQUAL_INT(0xC001, busService.Telemeter_TX_Data.Comm_Telemeter_TX_Count);
    TEST_ASSERT_EQUAL_INT(0, busService.Msg_TMTx_cnt);
    TEST_ASSERT_EQUAL_INT(0, retDataLen);
}

// TC005: 测试处理数据卷绕
void test_B1553_CMDHandle_should_Handle_Data_Wrap_When_Msg_DataWrap_cnt_Greater_Than_Zero(void) {
    // Arrange
    busService.Msg_DataWrap_cnt = 1;
    busService.Telemeter_TX_Data.Comm_DataWrap_cnt = 50;
    rt_CMDProc_ExpectAndReturn(&busService, 4);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(3, result);  // 4-1
    TEST_ASSERT_EQUAL_INT(51, busService.Telemeter_TX_Data.Comm_DataWrap_cnt);
    TEST_ASSERT_EQUAL_INT(0, busService.Msg_DataWrap_cnt);
    TEST_ASSERT_EQUAL_INT(0, retDataLen);
}

// TC006: 测试处理姿态轨道数据(更新模式)
void test_B1553_CMDHandle_should_Not_Handle_AltPos_Data_But_Decrement_Return_When_MsgBcst_PosAlt_cnt_Greater_Than_Zero_And_Work_Mode_Is_Update(void) {
    // Arrange
    busService.MsgBcst_PosAlt_cnt = 1;
    busService.Telemeter_TX_Data.Work_Mode_Flag = SYS_MODE_UPDATE;
    busService.Telemeter_TX_Data.Comm_Bcst_Count = 20;
    rt_CMDProc_ExpectAndReturn(&busService, 6);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(5, result);  // 6-1
    TEST_ASSERT_EQUAL_INT(21, busService.Telemeter_TX_Data.Comm_Bcst_Count);
    TEST_ASSERT_EQUAL_INT(0, busService.MsgBcst_PosAlt_cnt);
    TEST_ASSERT_EQUAL_INT(0, retDataLen);
}

// TC007: 测试处理姿态轨道数据(非更新模式)
void test_B1553_CMDHandle_should_Handle_AltPos_Data_When_MsgBcst_PosAlt_cnt_Greater_Than_Zero_And_Work_Mode_Is_Not_Update(void) {
    // Arrange
    busService.MsgBcst_PosAlt_cnt = 1;
    busService.Telemeter_TX_Data.Work_Mode_Flag = 1;  // 非更新模式
    busService.Telemeter_TX_Data.Comm_Bcst_Count = 30;
    rt_CMDProc_ExpectAndReturn(&busService, 1);
    rt_GetPosAltData_IgnoreAndReturn(&busService);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_ALTPOS, result);
    TEST_ASSERT_EQUAL_INT(31, busService.Telemeter_TX_Data.Comm_Bcst_Count);
    TEST_ASSERT_EQUAL_INT(0, busService.MsgBcst_PosAlt_cnt);
    TEST_ASSERT_EQUAL_INT(sizeof(busService.altPos_data), retDataLen);
    // 注意：在C语言中无法直接验证pRetData是否指向正确位置，因为它是局部变量的副本
}

// TC008: 测试处理时间数据
void test_B1553_CMDHandle_should_Handle_Time_Data_When_MsgBCST_DataTime_cnt_Greater_Than_Zero(void) {
    // Arrange
    busService.MsgBCST_DataTime_cnt = 1;
    busService.sat_time.second_H = 0x5678;
    busService.sat_time.second_L = 0xDEF0;
    rt_CMDProc_ExpectAndReturn(&busService, 2);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_TIME, result);
    TEST_ASSERT_EQUAL_INT(0x5678, busService.Telemeter_TX_Data.SatTime_second_H);
    TEST_ASSERT_EQUAL_INT(0xDEF0, busService.Telemeter_TX_Data.SatTime_second_L);
    TEST_ASSERT_EQUAL_INT(0, busService.MsgBCST_DataTime_cnt);
    TEST_ASSERT_EQUAL_INT(sizeof(struct BCST_SAT_time), retDataLen);
    // 注意：在C语言中无法直接验证pRetData是否指向正确位置
}

// TC009: 测试处理总线接收数据(_RecvBusData_proc返回CMDHANDLE_BUSCMD)
void test_B1553_CMDHandle_should_Handle_Bus_Data_When_Msg_Rx_cnt_Greater_Than_Zero_And_RecvBusData_proc_Returns_CMDHANDLE_BUSCMD(void) {
    // Arrange
    busService.Msg_Rx_cnt = 1;
    busService.DistrbTransDesc_bytes = 100;
    rt_CMDProc_ExpectAndReturn(&busService, 1);
    _RecvBusData_proc_ExpectAndReturn(&busService, CMDHANDLE_BUSCMD);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_BUSCMD, result);
    TEST_ASSERT_EQUAL_INT(0, busService.Msg_Rx_cnt);
    TEST_ASSERT_EQUAL_INT(100, retDataLen);
    // 注意：在C语言中无法直接验证pRetData是否指向正确位置
}

// TC010: 测试处理总线接收数据(_RecvBusData_proc返回其他值)
void test_B1553_CMDHandle_should_Handle_Bus_Data_Error_When_Msg_Rx_cnt_Greater_Than_Zero_And_RecvBusData_proc_Returns_Other_Value(void) {
    // Arrange
    busService.Msg_Rx_cnt = 1;
    rt_CMDProc_ExpectAndReturn(&busService, 1);
    _RecvBusData_proc_ExpectAndReturn(&busService, -1);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(-1, result);
    TEST_ASSERT_EQUAL_INT(0, busService.Msg_Rx_cnt);
    TEST_ASSERT_EQUAL_INT(0, retDataLen);
    // 注意：在C语言中无法直接验证pRetData是否为NULL
}

// TC011: 测试所有计数器都为0的情况
void test_B1553_CMDHandle_should_Return_CMDHANDLE_DONE_When_All_Counters_Are_Zero(void) {
    // Arrange
    rt_CMDProc_ExpectAndReturn(&busService, 1);
    
    // Act
    int result = B1553_CMDHandle(&busService, retData, &retDataLen);
    
    // Assert
    TEST_ASSERT_EQUAL_INT(CMDHANDLE_DONE, result);
}